package com.hf.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hf.entity.Users;
import com.hf.entity.UsersParam;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author eight
 * @since 2021-06-06
 */
public interface UsersMapper extends BaseMapper<Users> {

	@Select("select * from users where id=#{id}")
	public Users getUserByUserId(Integer id);

	@Update("update users set is_qiandao='已签到' where id=#{id}")
	public boolean updateIsQianDao(Integer id);

	@Update("update users set is_qiandao='已签退' where id=#{id}")
	public boolean updateIsQianTui(Integer id);

	/**
	 * 登录
	 * 
	 * @param loginName
	 * @param password
	 * @param isLockOut
	 * @return
	 */
	@ResultMap("usersResultMap")
	@Select("select * from users where login_name=#{loginName}")
	public Users userLogin(@Param("loginName") String loginName);

	/**
	 * 最后登录时间
	 * 
	 * @param loginName
	 * @param lastLoginTime
	 * @return
	 */

	@Update("update users set last_login_time=#{lastLoginTime} where id=#{id}")
	public boolean updateLastLoginTime(@Param("id") Integer id, @Param("lastLoginTime") String lastLoginTime);

	/**
	 * 查询错误密码的次数
	 * 
	 * @param loginName
	 * @param password
	 * @return
	 */
	@ResultMap("usersResultMap")
	@Select("select psd_wrong_num from users where login_name=#{loginName} and password=#{password}")
	public Users getPasswordNum(@Param("loginName") String loginName, @Param("password") String password);

	/**
	 * 输入错误密码，密码错误次数+1
	 * 
	 * @param loginName
	 * @return
	 */
	@Update("update users set psd_wrong_num=psd_wrong_num+1 where login_name=#{loginName}")
	public boolean updatepsdWrongNum(String loginName);

	/**
	 * 锁定账户， 根据登录名和错误密码次数修改是否锁定的状态为1， 0：未锁定，1：锁定
	 * 
	 * @param loginName
	 * @param lockTime
	 * @return
	 */
	@Update("update users set is_lock_out=1,psd_wrong_num=psd_wrong_num+1  where login_name=#{loginName}")
	public boolean updatelockNum(@Param("loginName") String loginName);

	/**
	 * 账号锁定时间
	 * 
	 * @param lockTime
	 * @return
	 */
	@Update("update users set lock_time=#{lockTime} where id=#{id}")
	public boolean setLockTime(@Param("lockTime") String lockTime, @Param("id") Integer id);

	/**
	 * 锁定
	 * 
	 * @param id
	 * @return
	 */

	@Update("update users set is_lock_out=1 where id=#{id}")
	public boolean setLock(Integer id);

	/**
	 * 解锁
	 * 
	 * @param id
	 * @return
	 */
	@Update("update users set is_lock_out=0, psd_wrong_num=0 where id=#{id}")
	public boolean setUnLock(Integer id);

	/**
	 * 查询咨询师
	 * 
	 * @param userParam
	 * @return
	 */
	@Select("select * from (select users.id,users.login_name,users.weight,roles.role_name from users,roles,user_roles where users.id=user_roles.user_id \n"
			+ "and roles.id=user_roles.role_id and user_roles.role_id in\n"
			+ "(select roles.id from roles where roles.role_name='咨询师')) a LEFT JOIN sign on a.id=sign.user_id and sign.id in(select MAX(sign.id) from sign  GROUP BY sign.user_id) order by weight desc")
	public List<Users> getziZunShi(UsersParam userParam);
	@Select("select users.id,users.login_name,users.weight,roles.role_name from users,roles,user_roles,sign where users.id=user_roles.user_id and sign.user_id=users.id\n"
			+ "	and roles.id=user_roles.role_id and  sign.id in(select MAX(sign.id) from sign  GROUP BY sign.user_id) and  user_roles.role_id=(select roles.id from roles where roles.role_name='咨询师') and TO_DAYS(sign_on_time)=TO_DAYS(NOW()) order by weight desc")
	public List<Users> getQianziZunShi(UsersParam userParam);


	
}
